package com.weichat.tool;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.model.Wx_access_token;
import com.service.Wx_access_tokenService;
import com.weichat.accessToken.Validate;
@Component
public class AccessTokentool {
	
	
	@Autowired
	Wx_access_tokenService wx_access_tokenService;
	
	
	
	public Map getNewAccessToken() throws IOException{
		Map m=new HashMap();
		Validate ve =new Validate();
		String result="";
		String accesstoken=wx_access_tokenService.selectByPrimaryKey(3).getAccessToken();
		//System.out.println("accesstoken==="+accesstoken);
		Map ip_list=ve.validate(accesstoken);
		System.out.println("errcode====="+ip_list.get("errcode"));
		Integer  a;
		if(ip_list.get("ip_list")!=null){
			m.put("rs", accesstoken);
		}
		if(ip_list.get("errcode")!=null){
			
			a=(Integer) ip_list.get("errcode");
			System.out.println("a============="+a);
			if (a==42001||a==40001||a==41001){
				accesstoken=WeiChatUtil.getAccessToken();
				Wx_access_token wn=new Wx_access_token();
				wn.setAccessToken(accesstoken);
				wn.setExpiresIn(7200);
				wn.setId(3);
				wx_access_tokenService.updateByPrimaryKeySelective(wn);
				m.put("rs", accesstoken);
			}
		}
		
		
		
		return m;
	}
	

}
